#!/bin/sh

#
# MikoPBX - free phone system for small business
# Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
#

# Get the current timestamp
time=$(date +%s);

# Create a unique filename using the timestamp
filename="/tmp/ps_result${time}";

# Retrieve process information using 'ps' command and save it to the file
/bin/ps -A -f -orss,pid,args | /bin/busybox grep -v '[0-9]m ' | /bin/busybox awk '{ print $1 "k ; " $2 " ; " $3 " " $4 " " $5 " " $6 " " $7 " " $8 }' > $filename;
/bin/ps -A -f -orss,pid,args | /bin/busybox grep '[0-9]m ' | /bin/busybox awk '{ print $1 " ; " $2 " ; " $3 " " $4 " " $5 " " $6 " " $7 " " $8 }' >> $filename;

# Sort the file contents in reverse order, ignoring lines with 0k or RSSk
/bin/busybox cat $filename | /bin/sort -r -h -f | /usr/bin/column -t -s ';' | /bin/busybox grep -v "^0k" | grep -v "^RSSk";

# Remove the temporary file
/bin/busybox rm -rf $filename;